<template>
  <div>
    <my-form-drawer
      :title="`设置密码有效期`"
      :width="500"
      :visible="visible"
      @ok="edit"
      @cancel="onClose"
    >
      <a-form-model ref="form" :model="form" :rules="rules">
        <a-form-model-item label="有效期设置">
          <a-switch v-model="form.configType" />
        </a-form-model-item>
        <a-form-model-item label="密码有效期（天）" prop="configValue">
          <a-input-number :disabled="form.configType === false" v-model="form.configValue" :formatter="limit" :min="1" />
        </a-form-model-item>
        <a-form-model-item>
          <span class="red">*开启密码有效期，密码修改成功后开始计时，旧密码失效后重新设置密码</span>
        </a-form-model-item>
      </a-form-model>
    </my-form-drawer>
  </div>
</template>

<script>
import moment from "moment";
import MyFormDrawer from "@/components/drawer/MyFormDrawer";
import { limitNumber } from "@/utils/common";
import { getModifyTime, setConfig} from "@/services/user";

export default {
  name: "UserPwd",
  components: { MyFormDrawer },
  data() {
    return {
      form: {
        id:0,
        configType: true,
        configValue: 90,
      },
      rules: {
        configValue:[{ required: true, message: '请输入时间', trigger: 'blur' },]
      },
      visible: false
    };
  },
  computed: {},
  created() {
    
  },
  mounted () {
    
  },
  methods: {
    moment,
    limit(value) {
      return limitNumber(value);
    },
    changeForm(state) {
      this.visible = state;
      this.getModifyTime();
    },
    onClose() {
      this.visible = false;
      this.$refs["form"].clearValidate();
    },
    getModifyTime(){
      getModifyTime().then((res) => {
        if(res.data.code === 0){
          console.log(res.data.data);
          this.form.configType = res.data.data.configType == 'Y' ? true : false;
          this.form.configValue = res.data.data.configValue;
          this.form.id = res.data.data.id;
        }   
      })
    },
    edit(){
      setConfig({
        id: this.form.id,
        configType: this.form.configType ? 'Y' : 'N',
        configValue: this.form.configValue
      }).then(res => {
        if(res.data.code == 0){
          this.$message.success('操作成功');
          this.onClose();
        }
      })
    }
  }
};
</script>

<style lang="less" scoped></style>
